/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author edoardo
 */
public class PasswordBank {


    public static String[] getAllUserMemorized() {
        String[] user = new String[0];
        String path = System.getProperty("user.dir") + "/bank";
        File f = new File(path);
        if (f.exists()) {
            try {
                FileInputStream fis = new FileInputStream(f);
                try {
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    try {
                        Vector v = (Vector) ois.readObject();
                        user = new String[v.size()];
                        for (int i = 0; i < v.size(); i++) {
                            User u = (User) v.elementAt(i);
                            user[i] = u.getUsername();
                        }
                        ois.close();
                        fis.close();
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return user;
    }

    public static String getPassByUsername(String username)
    {
        String pass = "";
        String path = System.getProperty("user.dir") + "/bank";
        File f = new File(path);
        if (f.exists())
        {
            try {
                FileInputStream fis = new FileInputStream(f);
                try {
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    try {
                        Vector v = (Vector) ois.readObject();
                        for(int i = 0;i<v.size();i++)
                        {
                            User u = (User) v.elementAt(i);
                            if (u.getUsername().equals(username))
                            {
                                pass = u.getPassword();
                            }
                        }
                        ois.close();
                        fis.close();
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return pass;
    }

    public static void addUser(User u)
    {
        String path = System.getProperty("user.dir") + "/bank";
        File f = new File(path);
        if (!f.exists())
        {
            try {
                f.createNewFile();
                FileOutputStream fos = new FileOutputStream(f);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                Vector v = new Vector();
                v.add(u);
                oos.writeObject(v);
                oos.close();
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else
        {
            try {
                FileInputStream fis = new FileInputStream(f);
                try {
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    try {
                        Vector v = (Vector) ois.readObject();
                        v.add(u);
                        ois.close();
                        fis.close();
                        FileOutputStream fos = new FileOutputStream(f);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);
                        oos.writeObject(v);
                        oos.close();
                        fos.close();
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PasswordBank.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
